using System;
using System.IO;
using System.Net;
using System.Text;
using Prime31;
using UnityEngine;

public class SkinExporterWindow : MonoBehaviour
{
	public static SkinExporterWindow instance;

	public UniWebView uniWebWiew;

	public ProductSkin choosedSkin;

	public GameObject toolbar;

	public GameObject SkinsHolder;

	public GameObject ChooseExportType;

	public GameObject ExportToPEHolder;

	public GameObject ExportToPEDialogueWindow;

	public GameObject ExportToPESkinSaved;

	public UISprite upSprite;

	public SkinExporterCategories categoriesPanel;

	public SkinsExporterProductPanel productsPanel;

	public MarketSkinMiniInfoPanel skinInfo;

	public SkinExporterPreviewController previewController;

	private void Awake()
	{
		instance = this;
		if (uniWebWiew == null)
		{
			uniWebWiew = GetComponent<UniWebView>();
		}
	}

	private void OnDestroy()
	{
		instance = null;
	}

	private void LoadCompleteEventHandler(UniWebView webView, bool success, string errorMessage)
	{
		if (!success)
		{
			BCWDebug.LogError("Load url error: " + errorMessage);
		}
	}

	private void LoadBeginEventHandler(UniWebView webView, string url)
	{
		SkinsHolder.SetActive(false);
		toolbar.SetActive(true);
	}

	public void RefreshWeb()
	{
		uniWebWiew.Reload();
	}

	public void ShowChooseSkinWindow()
	{
		BackToChooseSkin();
		if (GameUI.instance != null)
		{
			GameUI.instance.gameObject.SetActive(false);
		}
		Product equipedProduct = MarketController.Instance.GetEquipedProduct("Looks", string.Empty);
		categoriesPanel.Select(equipedProduct.subCategoryName);
		productsPanel.Select(equipedProduct.id, false);
		SkinExporterCategories.instance.InitButtons();
	}

	public void ShowChooseExportType()
	{
		ExportToPEHolder.SetActive(false);
		ChooseExportType.SetActive(true);
	}

	public void ExportChoosedSkinToMinecraftSite()
	{
		string text = ExportSkin(productsPanel.selectedProduct as ProductSkin);
		if (!string.IsNullOrEmpty(text))
		{
			Application.OpenURL(text);
		}
	}

	public void ShowExportChoosedSkinToMinecraftPE()
	{
		ExportToPEHolder.SetActive(true);
		ChooseExportType.SetActive(false);
		ExportToPEDialogueWindow.SetActive(true);
		ExportToPESkinSaved.SetActive(false);
	}

	public void ExportChoosedSkinToMinecraftPE()
	{
		if (!EtceteraAndroid.checkSelfPermission("android.permission.WRITE_EXTERNAL_STORAGE"))
		{
			EtceteraAndroid.requestPermissions(new string[1] { "android.permission.WRITE_EXTERNAL_STORAGE" });
		}
		else
		{
			ExportAndSetActiveChoosedSkinToMinecraftPE();
		}
	}

	private void ExportAndSetActiveChoosedSkinToMinecraftPE()
	{
		ExportSkin(productsPanel.selectedProduct as ProductSkin, true);
		ChooseExportType.SetActive(false);
		ExportToPEDialogueWindow.SetActive(false);
		ExportToPESkinSaved.SetActive(true);
	}

	public void BackToChooseSkin()
	{
		SkinsHolder.SetActive(true);
		toolbar.SetActive(false);
		ExportToPEHolder.SetActive(false);
		ChooseExportType.SetActive(false);
	}

	public void HideSkinExporter()
	{
		SkinsHolder.SetActive(false);
		toolbar.SetActive(false);
		ExportToPEHolder.SetActive(false);
		ChooseExportType.SetActive(false);
		categoriesPanel.Unselect();
		productsPanel.Unselect();
		if (GameUI.instance != null)
		{
			GameUI.instance.gameObject.SetActive(true);
		}
		if (SkinExporterABTestPanel.instance != null)
		{
			SkinExporterABTestPanel.instance.Show(true);
		}
	}

	private string ExportSkin(ProductSkin skin, bool saveToMinecraftPE = false)
	{
		Texture2D skinTexture = skin.GetSkinTexture();
		SkinsManager.SaveTextureWithName(skinTexture, "upload_Skin.png", false);
		string text = Path.Combine(SkinsManager._PathBase, "upload_Skin.png");
		if (saveToMinecraftPE)
		{
			EtceteraAndroid.saveImageToGallery(text, "upload_Skin");
			return string.Empty;
		}
		BCWDebug.Log("upload path=" + text);
		CookieContainer cookies = new CookieContainer();
		return UploadFileEx(text, "http://d-games.org/uploadimg.php", "userfile", "image/png", cookies);
	}

	private string UploadFileEx(string uploadfile, string url, string fileFormName, string contenttype, CookieContainer cookies)
	{
		WebClient webClient = new WebClient();
		if (fileFormName == null || fileFormName.Length == 0)
		{
			fileFormName = "file";
		}
		if (contenttype == null || contenttype.Length == 0)
		{
			contenttype = "application/octet-stream";
		}
		Uri requestUri = new Uri(url);
		string text = "----------" + DateTime.Now.Ticks.ToString("x");
		HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(requestUri);
		httpWebRequest.CookieContainer = cookies;
		httpWebRequest.ContentType = "multipart/form-data; boundary=" + text;
		httpWebRequest.Method = "POST";
		StringBuilder stringBuilder = new StringBuilder();
		stringBuilder.Append("--");
		stringBuilder.Append(text);
		stringBuilder.Append("\r\n");
		stringBuilder.Append("Content-Disposition: form-data; name=\"");
		stringBuilder.Append(fileFormName);
		stringBuilder.Append("\"; filename=\"");
		stringBuilder.Append(Path.GetFileName(uploadfile));
		stringBuilder.Append("\"");
		stringBuilder.Append("\r\n");
		stringBuilder.Append("Content-Type: ");
		stringBuilder.Append(contenttype);
		stringBuilder.Append("\r\n");
		stringBuilder.Append("\r\n");
		string s = stringBuilder.ToString();
		byte[] bytes = Encoding.UTF8.GetBytes(s);
		byte[] bytes2 = Encoding.UTF8.GetBytes("\r\n--" + text + "\r\n");
		FileStream fileStream = new FileStream(uploadfile, FileMode.Open, FileAccess.Read);
		long contentLength = bytes.Length + fileStream.Length + bytes2.Length;
		httpWebRequest.ContentLength = contentLength;
		Stream requestStream = httpWebRequest.GetRequestStream();
		requestStream.Write(bytes, 0, bytes.Length);
		byte[] array = new byte[checked((uint)Math.Min(4096, (int)fileStream.Length))];
		int num = 0;
		while ((num = fileStream.Read(array, 0, array.Length)) != 0)
		{
			requestStream.Write(array, 0, num);
		}
		requestStream.Write(bytes2, 0, bytes2.Length);
		BCWDebug.Log("webrequest=" + httpWebRequest);
		WebResponse response;
		try
		{
			response = httpWebRequest.GetResponse();
		}
		catch
		{
			return string.Empty;
		}
		Stream responseStream = response.GetResponseStream();
		StreamReader streamReader = new StreamReader(responseStream);
		return "http://www.minecraft.net/profile/skin/remote?url=" + streamReader.ReadToEnd();
	}
}
